home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_300 / 310_02 / symbol.c < prev    next >
C/C++ Source or Header  |  1990-04-18  |  3KB  |  139 lines

  1. /*
  2.     Little Smalltalk
  3.  
  4.         symbol creation - symbols are never deleted once created.
  5.         timothy a. budd, 10/84
  6. */
  7. /*
  8.     The source code for the Little Smalltalk System may be freely
  9.     copied provided that the source of all files is acknowledged
  10.     and that this condition is copied with each file.
  11.  
  12.     The Little Smalltalk System is distributed without responsibility
  13.     for the performance of the program and without any guarantee of
  14.     maintenance.
  15.  
  16.     All questions concerning Little Smalltalk should be addressed to:
  17.  
  18.         Professor Tim Budd
  19.         Department of Computer Science
  20.         Oregon State University
  21.         Corvallis, Oregon
  22.         97331
  23.         USA
  24. */
  25. # include <stdio.h>
  26. # include "object.h"
  27. # include "symbol.h"
  28.  
  29. /*
  30.     only one copy of symbol values are kept.
  31.     multiple copies of the same symbol point to the same
  32.     location.
  33.     sy_search will find, and if necessary insert, a string into
  34.     this common table 
  35. */
  36.  
  37. extern char x_str[];        /* initialized common string table */
  38. extern symbol *x_tab[];        /* initialized common symbols table */
  39. extern int x_tmax;        /* top of symbols table */
  40. extern char *walloc();        /* routine to allocate a new word */
  41. int ca_sym = 0;            /* symbol allocation counter */
  42.  
  43. /* sy_search performs a binary search of a symbol, is the main interface to
  44. the symbols routines */
  45. symbol *sy_search(word, insert)
  46. char *word;
  47. int  insert;
  48. {    register int i;
  49.     register int j;
  50.     register int k;
  51.     char *p;
  52.     symbol *new_y();
  53.  
  54.     for (i=1; i <= x_tmax; i <<= 1);
  55.     for (i >>= 1, j = i >>1, i--; ; j >>= 1) {
  56.         p = symbol_value(x_tab[i]);
  57.         if (word == p) return(x_tab[i]);
  58.         k = *word - *p;
  59.         if (!k) k = *(word+1) - *(p+1);
  60.         if (!k) k = strcmp(word, p);
  61.         if (!k)
  62.             return(x_tab[i]);
  63.         if (!j) break;
  64.         if (k < 0) i -= j;
  65.         else {
  66.             if ((i += j) > x_tmax) i = x_tmax;
  67.             }
  68.         }
  69.     if (insert) {
  70.         if (k > 0) i++;
  71.         if ((k = ++x_tmax) >= SYMTABMAX)
  72.             cant_happen(12);
  73.         for (; k > i; k--) {
  74.             x_tab[k] = x_tab[k-1];
  75.             }
  76.         /*fprintf(stderr,"adding %s\n", word);*/
  77.         x_tab[i] = new_y(walloc(word));
  78.         x_tab[i]->y_ref_count++; /* make sure not freed */
  79.         return(x_tab[i]);
  80.         }
  81.     else return((symbol *) 0);
  82. }
  83.  
  84. /* w_search performs a search for a word, not a symbol */
  85. char *w_search(word, insert)
  86. char *word;
  87. int insert;
  88. {    symbol *sym;
  89.  
  90.     sym = sy_search(word, insert);
  91.     if (sym)
  92.         return(symbol_value(sym));
  93.     else
  94.         return((char *) 0);
  95. }
  96.  
  97. /*---------------------------------------*/
  98.  
  99. static mstruct *fr_symbol = 0;        /* symbols free list */
  100. static symbol strspace[SYMINITSIZE];    /* initial symbols free list */
  101.  
  102. extern object *o_object;        /* common instance of Object */
  103. extern class *ArrayedCollection;
  104.  
  105. /* sym_init - initialize the symbols routine */
  106. sym_init() {
  107.     int  i;
  108.     symbol *p;
  109.     mstruct   *new;
  110.  
  111.     p = strspace;
  112.     for (i = 0; i < SYMINITSIZE; i++) {
  113.         new = (mstruct *) p;
  114.         new->mlink = fr_symbol;
  115.         fr_symbol = new;
  116.         p++;
  117.         }
  118. }
  119.  
  120. /* new_y is the internal routine for making new symbols */
  121. symbol *new_y(text)
  122. char *text;
  123. {    symbol *new;
  124.  
  125.     if (fr_symbol) {
  126.         new = (symbol *) fr_symbol;
  127.         fr_symbol = fr_symbol->mlink;
  128.         }
  129.     else {
  130.         ca_sym++;
  131.         new = structalloc(symbol);
  132.         }
  133.  
  134.     new->y_ref_count = 0;
  135.     new->y_size = SYMBOLSIZE;
  136.     new->y_value = text;
  137.     return(new);
  138. }
  139.